home *** CD-ROM | disk | FTP | other *** search
/ Aminet 37 / Aminet 37 (2000)(Schatztruhe)[!][Jun 2000].iso / Aminet / comm / bbs / cit_src_AD08.lha / tools.c < prev    next >
C/C++ Source or Header  |  1997-12-26  |  5KB  |  224 lines

  1. /*
  2. *               tools.c
  3. *
  4. * Random functions for both Ctdl and it's utilities.
  5. */
  6. /*
  7. *               history
  8. *
  9. * 90Aug26 HAW  Created.
  10. */
  11. #include "ctdl.h"
  12. /*
  13. *               Contents
  14. *
  15. *   NormStr()       Deletes leading trailing blanks etc.
  16. *   PrintPretty()       Print numbers prettily.
  17. *   normId()        Normalizes a node id.
  18. *   hash()          Hashes a string to an integer.
  19. *   CleanEnd()      Clears trailing blanks off.
  20. *   lbyte()         Finds the 0 byte of a string.
  21. */
  22. void  Do_Stack_Check(void);
  23. /*
  24. * NormStr()
  25. *
  26. * This function Deletes leading trailing blanks etc.
  27. */
  28. void NormStr(char *s)
  29.   {
  30.   char *save,*pc;
  31.   Do_Stack_Check();
  32.   pc = save = s;
  33.   /* find end of string and remove control characters   */
  34.   while (*pc)
  35.     {
  36.     if (*pc < ' ')   *pc = ' ';   /* zap tabs etc... */
  37.     pc++;
  38.  
  39.     };
  40.   pc--;                              /* no trailing spaces: */
  41.   while (pc>s  && *pc == ' ') pc--;
  42.   pc++;
  43.   *pc = '\0';                        /* no leading spaces: */
  44.   pc = s;
  45.   while( *pc == ' ') pc++;
  46.   if( pc != s )
  47.     {
  48.     while(*pc)*s++ = *pc++;
  49.     *s = '\0';
  50.  
  51.     };
  52.   s = save;                          /* no double blanks */
  53.   for (;  *s;)
  54.     {
  55.     if (*s == ' '   &&   *(s+1) == ' ')
  56.       {
  57.       for (pc=s;  *pc;  pc++)    *pc = *(pc+1);
  58.  
  59.       }
  60.     else s++;
  61.  
  62.     };
  63.  
  64.   }
  65. /*
  66. static long StartDiv = 1000000l;
  67. static char FirstFlag = TRUE;
  68. */
  69. /*
  70. * PrintPretty()
  71. *
  72. * This will pretty print a long with commas.
  73. */
  74. char *PrintPretty(long s, char *result)
  75.   {
  76.   Do_Stack_Check();
  77.   if( s > 1000 )
  78.     {
  79.     int i, j;
  80.     char *r;  /* result pointer */
  81.     char *p;  /* pretty print pointer */
  82.     char con_buf[20]; /* no number is bigger than this */
  83.     sprintf(con_buf,"%ld",s);
  84.     i = strlen(con_buf);
  85.     r = result;
  86.     p = con_buf;
  87.     j = i % 3;  /* number of characters the first time */
  88.     while( *p )
  89.       {
  90.       for(;j;j--) *r++ = *p++;
  91.       *r++ = ',';
  92.       j = 3;
  93.       };
  94.     r--;
  95.     *r = '\0';
  96.     }
  97.   else
  98.     {
  99.     sprintf(result,"%ld", s);   /* number is 3 digits or less */
  100.     };
  101.   return result;
  102. /*
  103.   if (StartDiv == 1)
  104.     {
  105.     sprintf(result, FirstFlag ? "%ld" : "%03ld", s);
  106.     FirstFlag = TRUE;
  107.     StartDiv  = 1000000l;
  108.     return result;
  109.  
  110.     }
  111.   if (s >= StartDiv)
  112.     {
  113.     sprintf(result, FirstFlag ? "%ld," : "%03ld,", s / StartDiv);
  114.     FirstFlag = FALSE;
  115.     s %= StartDiv;
  116.     StartDiv /= 1000;
  117.     PrintPretty(s, result + strlen(result));
  118.  
  119.     }
  120.   else
  121.     {
  122.     StartDiv /= 1000;
  123.     PrintPretty(s, result);
  124.  
  125.     }
  126.   return result;
  127. */
  128.   }
  129. /*
  130. * normId()
  131. *
  132. * This function normalizes a node id.
  133. ***duplicate of lib source
  134. char normId(label source, label dest)
  135.   {
  136.   int digitcount = 0;
  137.   while (!isalpha(*source) && *source)
  138.   source++;
  139.   if (!*source) return FALSE;
  140.   *dest++ = toUpper(*source++);
  141.   while (!isalpha(*source) && *source)
  142.   source++;
  143.   if (!*source) return FALSE;
  144.   *dest++ = toUpper(*source++);
  145.   while (*source)
  146.     {
  147.     if (isdigit(*source))
  148.       {
  149.       *dest++ = *source;
  150.       digitcount++;
  151.       }
  152.       source++;
  153.       }
  154.       *dest = '\0';
  155.       return (digitcount > 8);
  156.       }
  157.       Commented out NormId */
  158.       /*
  159.       * hash()
  160.       *
  161.       * This function hashes a string to an integer.
  162.       */
  163.       UNS_16 hash(char *str)
  164.         {
  165.         UNS_16  h, shift;
  166.         Do_Stack_Check();
  167.         for (h=shift=0;  *str;  shift=(shift+1)&7, str++)
  168.           {
  169.           h ^= (toUpper(*str)) << shift;
  170.  
  171.           }
  172.         return h;
  173.  
  174.         }
  175.       /*
  176.       * CleanEnd()
  177.       *
  178.       * This function cleans up a message trailer for later display via Continue or
  179.       * .EH.  Inspired by Glen Heinz (MacCitadel).
  180.       */
  181.       char *CleanEnd(char *text)
  182.         {
  183.         char *ptr;
  184.         int  wc, lc;    /* Word Count and Letter Count */
  185.         Do_Stack_Check();
  186.         if (strLen(text) == 0) return text;
  187.         ptr = lbyte(text) - 1;      /* End of text of msg */
  188.         /*
  189.         * Strip trailing whitespace.  We structure the loop this
  190.         * way to avoid any chance of accidentally accessing memory outside
  191.         * of the memory area.
  192.         */
  193.         while (ptr != text - 1)
  194.           {
  195.           if (!(*ptr == ' ' || *ptr == NEWLINE || *ptr == TAB)) break;
  196.           ptr--;
  197.  
  198.           }
  199.         ptr++;  /* point at byte following last significant character */
  200.         *ptr = 0;   /* tie it off with a NULL */
  201.         /* Now we want to find a "preferred place" */
  202.         for (wc = lc = 0, ptr--; wc < 4 && ptr > text && lc < 35; ptr--, lc++)
  203.           {
  204.           if (*ptr == ' ') wc++;
  205.           if (*ptr == NEWLINE) break;     /* can't go beyond embedded NEWLINE */
  206.  
  207.           }
  208.         if (ptr == text) return ptr;    /* if msg is empty or < 35 chars long */
  209.         /* else */       return (ptr + 1);  /* else return "favored" spot */
  210.  
  211.         }
  212.       /*
  213.       * lbyte()
  214.       *
  215.       * This function finds the 0 byte of a string, returns pointer to it...
  216.       */
  217.       char *lbyte(char *l)
  218.         {
  219.         Do_Stack_Check();
  220.         while (*l) l++;
  221.         return l;
  222.  
  223.         }
  224.